/*
 * @Author: meetqy
 * @since: 2019-09-24 14:23:27
 * @lastTime: 2019-10-25 15:11:23
 * @LastEditors: meetqy
 */
import 'package:flutter/material.dart';
import 'package:test_xzkdemo/utils/common/Request.dart';
import 'package:test_xzkdemo/utils/common/loading.dart';
// import 'package:test_xzkdemo/utils/common/toast.dart';
//import 'package:shared_preferences/shared_preferences.dart';

export './Icon.dart';
export './customAppbar.dart';

//toast 工具包
import 'package:fluttertoast/fluttertoast.dart';
//颜色工具包
import 'package:test_xzkdemo/utils/common/colorUtil.dart';

/// global
class G {
  //GlobalKey 使用了一个静态常量 Map 来保存它对应的 Element。
  //用来保持，对 stateFullWeight的状态
  static final GlobalKey<NavigatorState> navigatorKey=GlobalKey();
  /// toolbar routeName
  static final List toobarRouteNameList = ['/', '/menu', '/order', '/shopping_cart', '/mine'];

  /// 通用正则
  static final Map regExpRules = {
    /// 替换规格
    "specName": '规格:|温度:|糖度:|奶油:|无'
  };

  /// 本地存储
//  static final Future<SharedPreferences> prefs = SharedPreferences.getInstance();

  /// 初始化Request
  static final Request dio = Request();

  /// 初始化loading
  static final Loading loading = Loading();

  /// 初始化toask
  // static final Toast toast = Toast();

  /// 手动延时
  static sleep({ int milliseconds = 1000 }) async => await Future.delayed(Duration(milliseconds: milliseconds));

  /// 下拉刷新样式

  /// 获取当前的state
  static NavigatorState getCurrentState() => navigatorKey.currentState;

  /// 获取当前的context
  static BuildContext getCurrentContext() => navigatorKey.currentContext;

  /// 获取屏幕上下边距
  /// 用于兼容全面屏，刘海屏
  static EdgeInsets screenPadding(BuildContext context) => MediaQuery.of(context).padding;

  /// 获取屏幕宽度  getCurrentContext()是空的，导致屏幕宽高无法获取
  static double screenWidth(BuildContext context) => MediaQuery.of(context).size.width;

  /// 获取屏幕高度
  static double screenHeight(BuildContext context) => MediaQuery.of(context).size.height;

  /// 跳转页面使用 G.pushNamed
  static void pushNamed(String routeName, {Object arguments}){
    // 如果跳转到toolbar页面  不能返回
    if(toobarRouteNameList.indexOf(routeName) > -1) {
      getCurrentState().pushReplacementNamed(routeName, arguments: arguments,);
    } else {
      getCurrentState().pushNamed(routeName, arguments: arguments);
    }
  }

  /// 返回页面
  static void pop() => getCurrentState().pop();

  /// 底部border
  /// ```
  /// @param {Color} color
  /// @param {bool} show  是否显示底部border
  /// ```
  static Border borderBottom({Color color, bool show = true}){
    return Border(
      bottom: BorderSide(
        color: (color == null || !show)  ? (show ? Color.fromARGB(242, 242, 242, 1) : Colors.transparent) : color,
        width: 1
      )
    );
  }

/*
 * 显示吐司工具提示
 */
static showToast(String msg,ToastGravity toastGravity){
  if(toastGravity == null){
    toastGravity = ToastGravity.CENTER;
  }
  Fluttertoast.showToast(
    //表示将数据绑定到字符串中
        msg: "$msg",
        toastLength: Toast.LENGTH_SHORT,
        gravity: toastGravity,
        timeInSecForIos: 1,
        backgroundColor: ColorUtil.hexToColor("#111111"),
        textColor: ColorUtil.hexToColor("#ffffff")
    );
}
/*
 * 显示吐司工具提示
 */
static showToastBottom(String msg){ 
  Fluttertoast.showToast(
    //表示将数据绑定到字符串中
        msg: "$msg",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.BOTTOM,
        timeInSecForIos: 1,
        backgroundColor: ColorUtil.hexToColor("#111111"),
        textColor: ColorUtil.hexToColor("#ffffff")
    );
}
}